﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

[assembly: CLSCompliant(true)]
namespace ArriviSoft.SlotMachine.Presenter
{
    public class SlotMachinePresenter
    {
        private SlotMachine.Model.SlotMachine machine;
        private ISlotMachineView view;

        public SlotMachinePresenter(ISlotMachineView view)
        {
            SlotMachine.Model.ISlotGenerator generator = new SlotMachine.Model.SlotGenerator(3, 4);
            SlotMachine.Model.ISlotValidator validator = new SlotMachine.Model.SlotValidator();

            this.machine = new SlotMachine.Model.SlotMachine(generator, validator);
            this.view = view;
        }

        public SlotMachinePresenter(SlotMachine.Model.SlotMachine machine, ISlotMachineView view)
        {
            this.machine = machine;
            this.view = view;
        }

        public void InitializeView()
        {
            view.DisplayCoinsLeft(this.Coins);
        }

        public int Coins
        {
            get { return this.machine.Coins; }
        }

        public bool CanPlay
        {
            get { return this.Coins > 0; }
        }

        public void InsertCoins(int coins)
        {
            this.machine.InsertCoins(coins);
            this.view.DisplayCoinsLeft(this.Coins);
        }

        public void Play()
        {
            SlotMachine.Model.Outcome result = this.machine.Play();
            this.view.DisplayCombination(result.Combination());

            if (result.Prize > 0)
            {
                this.view.DisplayMessage(string.Format("YOU WIN {0} $", result.Prize));
                this.InsertCoins(result.Prize);
            }
            else
            {
                if (this.Coins > 0)
                {
                    this.view.DisplayMessage("Bad lucky! Try again...");
                }
                else
                {
                    this.view.DisplayMessage("No more coins left!!!");
                }
            }
            this.view.DisplayCoinsLeft(this.Coins);
        }
    } 
}
